-- ============================================
-- STOP LA BARRE - ComputerCraft / CC:Tweaked
-- Nécessite : Advanced Computer + Advanced Monitor (couleur)
-- ============================================

local MISE = 1
local points = 100
local TICK = 0.05  -- vitesse de deplacement du curseur

local mon = peripheral.find("monitor")
if not mon then
    error("Aucun monitor trouve ! Colle un Advanced Monitor a cote du computer.")
end

mon.setTextScale(0.5)
local prevTerm = term.redirect(mon)
local w, h = term.getSize()

local barY = math.floor(h / 2)
local barLeft, barRight = 3, w - 2
local barWidth = barRight - barLeft + 1

local function centerText(y, text, textColor, bgColor)
    term.setBackgroundColor(bgColor or colors.black)
    term.setTextColor(textColor or colors.white)
    local x = math.floor((w - #text) / 2) + 1
    term.setCursorPos(x, y)
    term.write(text)
end

local function fillArea(x1, y1, x2, y2, bg)
    term.setBackgroundColor(bg)
    for y = y1, y2 do
        term.setCursorPos(x1, y)
        term.write(string.rep(" ", x2 - x1 + 1))
    end
end

-- Zones de gain, symetriques par rapport au centre de la barre
-- (bornes en fraction de la largeur de la barre)
local ZONES = {
    { from = 0.00, to = 0.12, mult = 0 },
    { from = 0.12, to = 0.28, mult = 1 },
    { from = 0.28, to = 0.42, mult = 3 },
    { from = 0.42, to = 0.58, mult = 8 },  -- zone centrale, jackpot
    { from = 0.58, to = 0.72, mult = 3 },
    { from = 0.72, to = 0.88, mult = 1 },
    { from = 0.88, to = 1.00, mult = 0 },
}

local function zoneColor(mult)
    if mult >= 8 then return colors.magenta
    elseif mult >= 3 then return colors.orange
    elseif mult >= 1 then return colors.yellow
    else return colors.gray end
end

local function drawTrack()
    for _, zone in ipairs(ZONES) do
        local x1 = barLeft + math.floor(zone.from * barWidth)
        local x2 = barLeft + math.floor(zone.to * barWidth) - 1
        term.setBackgroundColor(zoneColor(zone.mult))
        term.setTextColor(colors.white)
        for x = x1, x2 do
            term.setCursorPos(x, barY)
            term.write(" ")
        end
        local label = "x" .. zone.mult
        local lx = x1 + math.floor(((x2 - x1 + 1) - #label) / 2)
        if lx >= x1 then
            term.setCursorPos(lx, barY)
            term.write(label)
        end
    end
end

local function zoneAt(fraction)
    for _, zone in ipairs(ZONES) do
        if fraction >= zone.from and fraction < zone.to then
            return zone.mult
        end
    end
    return ZONES[#ZONES].mult
end

local function drawCursor(x, hide)
    term.setBackgroundColor(colors.black)
    term.setTextColor(colors.white)
    term.setCursorPos(x, barY - 1)
    term.write(hide and " " or "v")
end

local function drawHeader()
    fillArea(1, 1, w, barY - 2, colors.black)
    centerText(1, "=== CASINO COMPUTERCRAFT ===", colors.yellow)
    centerText(2, "STOP LA BARRE", colors.white)
    centerText(3, "Points : " .. points, colors.lime)
end

-- === UNE MANCHE ===
local function playRound()
    if points < MISE then
        drawHeader()
        drawTrack()
        centerText(h - 1, "PLUS ASSEZ DE POINTS !", colors.red)
        sleep(2)
        return
    end
    points = points - MISE

    drawHeader()
    drawTrack()
    centerText(h - 1, "Touche l'ecran pour arreter la barre !", colors.lightGray)

    local pos = 0          -- position du curseur (0 a barWidth-1)
    local direction = 1
    local speed = 1
    local stopped = false
    local finalX = barLeft

    local timerId = os.startTimer(TICK)
    while not stopped do
        local event, p1 = os.pullEvent()

        if event == "timer" and p1 == timerId then
            drawCursor(barLeft + pos, true) -- efface l'ancienne position
            pos = pos + direction * speed
            if pos >= barWidth - 1 then
                pos = barWidth - 1
                direction = -1
            elseif pos <= 0 then
                pos = 0
                direction = 1
            end
            drawCursor(barLeft + pos, false)
            timerId = os.startTimer(TICK)

        elseif event == "monitor_touch" then
            finalX = barLeft + pos
            stopped = true
        end
    end

    local fraction = (finalX - barLeft) / barWidth
    local mult = zoneAt(fraction)
    local gain = MISE * mult
    points = points + gain

    drawHeader()
    local msg, msgColor
    if gain > 0 then
        msg, msgColor = "Arrete en x" .. mult .. " ! +" .. gain, colors.lime
    else
        msg, msgColor = "Zone morte... perdu !", colors.red
    end
    centerText(h - 1, msg, msgColor)
    sleep(2.5)
end

-- === ECRAN D'ACCUEIL ===
local button = {}

local function drawIdle()
    term.setBackgroundColor(colors.black)
    term.clear()
    drawHeader()
    drawTrack()

    local label = "MISER (" .. MISE .. " point) ET LANCER"
    local bw = #label + 4
    local x1 = math.max(1, math.floor((w - bw) / 2) + 1)
    local y = h - 1
    term.setBackgroundColor(colors.green)
    term.setTextColor(colors.white)
    for x = x1, math.min(w, x1 + bw - 1) do
        term.setCursorPos(x, y)
        term.write(" ")
    end
    term.setCursorPos(x1 + 2, y)
    term.write(label)
    button = { x1 = x1, x2 = x1 + bw - 1, y = y }
end

-- === PROGRAMME PRINCIPAL ===
drawIdle()

while true do
    local event, side, x, y = os.pullEvent("monitor_touch")
    if x >= button.x1 and x <= button.x2 and y == button.y then
        playRound()
        drawIdle()
    end
end